package com.jdd.modules.area.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jdd.common.api.vo.Result;
import com.jdd.common.aspect.annotation.*;
import com.jdd.common.system.query.QueryGenerator;
import com.jdd.common.system.vo.LoginUser;
import com.jdd.common.util.LoginUserUtils;
import com.jdd.common.util.SnowFlakeUtil;
import com.jdd.modules.area.entity.ParkAreaInfo;
import com.jdd.modules.area.service.IParkAreaInfoService;
import com.jdd.modules.parking.entity.ConfigParkInfo;
import com.jdd.modules.parking.service.IConfigParkInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Description: 停车场区域信息表
 * @Author: jdd
 * @Date: 2021-07-15
 * @Version: V1.0
 */
@Slf4j
@Api(tags = "停车场区域信息表")
@RestController
@RequestMapping("/area/parkAreaInfo")
public class ParkAreaInfoController {
    @Resource
    private IParkAreaInfoService parkAreaInfoService;
    @Resource
    private IConfigParkInfoService configParkInfoService;

    /**
     * 分页列表查询
     *
     * @param parkAreaInfo
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "停车场区域信息表-分页列表查询")
    @ApiOperation(value = "停车场区域信息表-分页列表查询", notes = "停车场区域信息表-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<ParkAreaInfo>> queryPageList(ParkAreaInfo parkAreaInfo,
                                                     @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                     @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                     HttpServletRequest req) {
        Result<IPage<ParkAreaInfo>> result = new Result<IPage<ParkAreaInfo>>();
        QueryWrapper<ParkAreaInfo> queryWrapper = QueryGenerator.initQueryWrapper(parkAreaInfo, req.getParameterMap());
        Page<ParkAreaInfo> page = new Page<ParkAreaInfo>(pageNo, pageSize);
        IPage<ParkAreaInfo> pageList = parkAreaInfoService.page(page, queryWrapper);
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }

    @AutoLog(value = "停车场区域信息表-获取指定车场的所有区域信息")
    @ApiOperation(value = "停车场区域信息表-获取指定车场的所有区域信息", notes = "停车场区域信息表-获取指定车场的所有区域信息")
    @GetMapping(value = "/getAreaInfos")
    public Result<?> getAreaInfos(ParkAreaInfo parkAreaInfo) {
        try {
            List<ParkAreaInfo> parkAreaInfos = parkAreaInfoService.list();
            return Result.ok(parkAreaInfos);
        } catch (Exception e) {
            return Result.error("获取区域信息失败");
        }

    }

    /**
     * 添加
     *
     * @param parkAreaInfo
     * @return
     */
    @AutoLog(value = "停车场区域信息表-添加")
    @ApiOperation(value = "停车场区域信息表-添加", notes = "停车场区域信息表-添加")
    @PostMapping(value = "/add")
    @OperationLogDetail(detail = "停车场区域信息表，添加", level = LogLevelType.ONE, operationType = OperationType.INSERT, operationUnit = OperationUnit.UNKNOWN)
    public Result<ParkAreaInfo> add(@RequestBody ParkAreaInfo parkAreaInfo) {
        Result<ParkAreaInfo> result = new Result<ParkAreaInfo>();
        try {
            ConfigParkInfo configParkInfo = configParkInfoService.findConfigParkInfoByParkCode();
            if (ObjectUtil.isNull(configParkInfo)) {
                return Result.error(510, "车辆信息配置为空，请检查");
            }
            LoginUser loginUser = LoginUserUtils.getLoginUser();
            if (StringUtils.isNotBlank(loginUser.getParkCode())) {
                parkAreaInfo.setParkCode(loginUser.getParkCode());
            }
            parkAreaInfo.setCreateUserName(loginUser.getRealname());
            parkAreaInfo.setCreateTime(new Date());
            parkAreaInfo.setId(SnowFlakeUtil.getId());
            parkAreaInfoService.save(parkAreaInfo);
            result.success("添加成功！");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.error500("操作失败");
        }
        return result;
    }

    /**
     * 编辑
     *
     * @param parkAreaInfo
     * @return
     */
    @AutoLog(value = "停车场区域信息表-编辑")
    @ApiOperation(value = "停车场区域信息表-编辑", notes = "停车场区域信息表-编辑")
    @PutMapping(value = "/edit")
    @OperationLogDetail(detail = "停车场区域信息表，编辑", level = LogLevelType.ONE, operationType = OperationType.UPDATE, operationUnit = OperationUnit.UNKNOWN)
    public Result<ParkAreaInfo> edit(@RequestBody ParkAreaInfo parkAreaInfo) {
        Result<ParkAreaInfo> result = new Result<ParkAreaInfo>();
        ParkAreaInfo parkAreaInfoEntity = parkAreaInfoService.getById(parkAreaInfo.getId());
        if (parkAreaInfoEntity == null) {
            result.error500("未找到对应实体");
        } else {
            LoginUser loginUser = LoginUserUtils.getLoginUser();
            parkAreaInfo.setUpdateTime(new Date());
            parkAreaInfo.setUpdateUserId(loginUser.getUsername());
            parkAreaInfo.setUpdateUserName(loginUser.getRealname());
            boolean ok = parkAreaInfoService.updateById(parkAreaInfo);
            if (ok) {
                result.success("修改成功!");
            }
        }

        return result;
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "停车场区域信息表-通过id删除")
    @ApiOperation(value = "停车场区域信息表-通过id删除", notes = "停车场区域信息表-通过id删除")
    @DeleteMapping(value = "/delete")
    @OperationLogDetail(detail = "停车场区域信息表，通过id删除", level = LogLevelType.ONE, operationType = OperationType.DELETE, operationUnit = OperationUnit.UNKNOWN)
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        try {
            parkAreaInfoService.removeById(id);
        } catch (Exception e) {
            log.error("删除失败", e.getMessage());
            return Result.error("删除失败!");
        }
        return Result.ok("删除成功!");
    }
    /**
     * 通过id查询
     * @param id
     * @return
     */
    @AutoLog(value = "查询权限组下所有区域信息")
    @ApiOperation(value="查询权限组下所有区域信息", notes="查询权限组下所有区域信息")
    @GetMapping(value = "/queryByPermissionIdAreaList")
    public Result<?> queryByPermissionIdAreaList(@RequestParam(name="id",required=true) String id) {
        Result<List<ParkAreaInfo>> result = new Result<List<ParkAreaInfo>>();
        List<ParkAreaInfo> areaInfoList = parkAreaInfoService.getByPermissionIdAndAreaInfoList(id);
        if(areaInfoList==null) {
            result.error500("未找到对应实体");
        }else {
            result.setResult(areaInfoList);
            result.setSuccess(true);
        }
        return result;
    }
    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "停车场区域信息表-批量删除")
    @ApiOperation(value = "停车场区域信息表-批量删除", notes = "停车场区域信息表-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    @OperationLogDetail(detail = "停车场区域信息表，批量删除", level = LogLevelType.ONE, operationType = OperationType.DELETE, operationUnit = OperationUnit.UNKNOWN)
    public Result<ParkAreaInfo> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        Result<ParkAreaInfo> result = new Result<ParkAreaInfo>();
        if (ids == null || "".equals(ids.trim())) {
            result.error500("参数不识别！");
        } else {
            this.parkAreaInfoService.removeByIds(Arrays.asList(ids.split(",")));
            result.success("删除成功!");
        }
        return result;
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "停车场区域信息表-通过id查询")
    @ApiOperation(value = "停车场区域信息表-通过id查询", notes = "停车场区域信息表-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<ParkAreaInfo> queryById(@RequestParam(name = "id", required = true) String id) {
        Result<ParkAreaInfo> result = new Result<ParkAreaInfo>();
        ParkAreaInfo parkAreaInfo = parkAreaInfoService.getById(id);
        if (parkAreaInfo == null) {
            result.error500("未找到对应实体");
        } else {
            result.setResult(parkAreaInfo);
            result.setSuccess(true);
        }
        return result;
    }

}
